<template>
  <a-modal
    :title="isShow ? '修改商户信息' : '新建商户信息'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
    okText="确定"
    cancelText="取消"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="名称">
          <a-input
            v-decorator="['name', {rules: [{required: true, message: '请填写名称'}]}]" 
            placeholder="名称"
          />
        </a-form-item>
                
        <a-form-item label="标识">
          <a-select
            v-decorator="['type', {rules: [{required: true, message: '请选择标识'}]}]"
            placeholder="请选择标识"
          >
		  <!-- 1.分销商,2.渠道商,3.批发商 -->
            <a-select-option :value="1">分销商</a-select-option>
            <a-select-option :value="2">渠道商</a-select-option>
            <a-select-option :value="3">批发商</a-select-option>
          </a-select>
        </a-form-item>
        
        <!-- <a-form-item label="ak">
          <a-input
            v-decorator="['ak', {rules: [{required: true, message: '请填写ak'}]}]"
            placeholder="ak"
          />
        </a-form-item>

        <a-form-item label="sk">
          <a-input
            v-decorator="['sk', {rules: [{required: true, message: '请填写sk'}]}]"
            placeholder="sk"
          />
        </a-form-item> -->

        <a-form-item label="授信额度">
          <a-input
            v-decorator="['credit_line', {rules: [{required: true, message: '请填写授信额度'}]}]"
            placeholder="授信额度"
          />
        </a-form-item>
        
        <a-form-item label="可用余额">
          <a-input
            v-decorator="['available_balance', {rules: [{required: true, message: '请填写可用余额'}]}]"
            placeholder="可用余额"
          />
        </a-form-item>

        <a-form-item label="账号">
          <a-input
            v-decorator="['username', {rules: [{required: true, message: '请填写账号'}]}]"
            placeholder="请填写账号"
          />
        </a-form-item>
        
        <a-form-item label="密码">
          <a-input type="password"
            v-decorator="['password', {rules: [{required: true, message: '请填写密码'}]}]"
            placeholder="请填写密码"
          />
        </a-form-item>

        <a-form-item label="绑定业务员">
          <a-select
          v-decorator="['user_id', {rules: [{required: true, message: '请选择！'}]}]" 
            placeholder="请选择"
            option-filter-prop="children"
            :filter-option="filterOption"
          >
            <a-select-option :value="item.id" v-for="(item) in userList" :key="item.id">
                {{item.username}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="商户有效时间">
            <a-date-picker
              style="width:100%"
              v-decorator="['endtime', {rules: [{required: true, message: '请填写商户的有效时间'}]}]"
              format="YYYY-MM-DD"
              placeholder="请填写商户的有效时间"
            />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['id', 'name', 'type', 'ak', 'sk',
'endtime', 'credit_line', 'acredit_line', 'available_balance','recharge', 'deduction', 'username', 'password','user_id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    userList:{
      type: Array,
      default: () => null,
    }
  },
  components: {
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      isShow: false,
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
      
      if(this.model && this.model.id > 0) {
        this.isShow = true
        this.form.setFieldsValue({
          password: '',
        });
      } else {
        this.isShow = false
      }
    })
  },
  mounted() {
  },
  methods: {
    // 搜素模糊查询
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
  }
}
</script>
<style scoped>

</style>
